package com.example.Config;

import com.example.Interceptor.TestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
// 继承自WebMvcConfigurerAdapter 类在SpringBOot 2.0 之后废弃了
public class WebInterceptorConfig extends WebMvcConfigurationSupport {

//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        // 将拦截器注册到工厂，并拦截所有的请求.
//        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**")
//                .excludePathPatterns("/doc.html")
//                .excludePathPatterns("/swagger-ui.html")
//                .excludePathPatterns("/configuration/ui")
//                .excludePathPatterns("/swagger-resources")
//                .excludePathPatterns("/configuration/security")
//                .excludePathPatterns("/v2/api-docs")
//                .excludePathPatterns("/error")
//                .excludePathPatterns("/webjars/**");
//        super.addInterceptors(registry);
//    }

    /**
     *拦截器配置注册，同时放开拦截器对于静态资源的拦截，包括swagger 2的 api 页面
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("拦截器拦截请求-------------------------");
        //配置登录拦截器
        registry.addInterceptor(new TestInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**")
                .excludePathPatterns("/user/*");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
